package com.scyshkj.university.wechat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.validation.Validator;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ChengDuUniversityWechatPayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChengDuUniversityWechatPayApplication.class, args);
    }

    /**
     * 注册全局validator
     *
     * @param applicationContext 应用上下文
     */
    @Bean
    public ConfigurableWebBindingInitializer initializer(ApplicationContext applicationContext) {
        ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
        initializer.setValidator((Validator) applicationContext.getBean("webGlobalValidator"));
        return initializer;
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//ms
        factory.setConnectTimeout(15000);//ms
        return factory;
    }
}
